﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TeamBuy.AccessWebUI
{
    public partial class Buy : System.Web.UI.Page
    {
        public static int currentDealID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (Request.Form["ctl00$ctl00$cphMain$cphDefault$Main_DealDetail1$lvDeal$ctrl0$txtDealID"] != null && Request.Form["ctl00$ctl00$cphMain$cphDefault$Main_DealDetail1$lvDeal$ctrl0$txtDealID"] != "Deal")
                {
                    currentDealID = Convert.ToInt32(Request.Form["ctl00$ctl00$cphMain$cphDefault$Main_DealDetail1$lvDeal$ctrl0$txtDealID"]);
                    TeamBuy.AccessService.TeamBuyDealService getDeal = new AccessService.TeamBuyDealService();
                    DataTable deal = getDeal.GetDealByID(Convert.ToInt32(Request.Form["ctl00$ctl00$cphMain$cphDefault$Main_DealDetail1$lvDeal$ctrl0$txtDealID"]));
                    ltlPrice.Text = deal.Rows[0]["NowPrice"].ToString();
                    ltlTotal.Text = deal.Rows[0]["NowPrice"].ToString();
                    ltlDealTitle.Text = deal.Rows[0]["DealTitle"].ToString();
                    //if (Convert.ToBoolean(deal.Rows[0]["IsNeedTransferFee"]) == true)
                    //{
                    //    ltlTransferFee.Text = deal.Rows[0]["TransferFee"].ToString();
                    //}
                }
                else if (Request.Form["ctl00$ctl00$cphMain$cphUser$txtOrderID"] != null && Request.Form["ctl00$ctl00$cphMain$cphUser$txtOrderID"] != "")
                {
                    TeamBuy.AccessService.TeamBuyOrderService modifyOrder = new AccessService.TeamBuyOrderService();
                    DataTable dtOrder = modifyOrder.GetOrderInfoByID(Convert.ToInt32(Request.Form["ctl00$ctl00$cphMain$cphUser$txtOrderID"]));
                    if (dtOrder.Rows.Count == 1)
                    {
                        if (Convert.ToBoolean(dtOrder.Rows[0]["IsHadCard"]) == true)
                        {
                            TeamBuy.AccessService.TeamBuyCardService setCard = new AccessService.TeamBuyCardService();
                            DataTable dtCard = setCard.GetCardByCardNumber(Request.Form["ctl00$ctl00$cphMain$cphUser$txtCardNumber"].ToString());
                            ltlCardNumber.Text = dtCard.Rows[0]["CardNumber"].ToString();
                            ltlCardPrice.Text = Convert.ToDouble(dtCard.Rows[0]["CardPrice"]).ToString();
                        }
                        ltlDealTitle.Text = dtOrder.Rows[0]["DealTitle"].ToString();
                        ltlPrice.Text = dtOrder.Rows[0]["NowPrice"].ToString();
                        txtQuantity.Text = dtOrder.Rows[0]["Quantity"].ToString();
                        ltlTotalAll.Text = dtOrder.Rows[0]["TotalPrice"].ToString();
                        txtMobile.Text = dtOrder.Rows[0]["MobileNumber"].ToString();
                    }

                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
            ltlTotal.Text = (Convert.ToInt32(txtQuantity.Text) * Convert.ToDouble(ltlPrice.Text)).ToString();
            ltlTotalAll.Text = (Convert.ToInt32(txtQuantity.Text) * Convert.ToDouble(ltlPrice.Text) - Convert.ToDouble(ltlCardPrice.Text) + Convert.ToDouble(ltlTransferFee.Text)).ToString();
        }

        protected void btnAddCard_Click(object sender, EventArgs e)
        {
            TeamBuy.AccessService.TeamBuyCardService useCard = new AccessService.TeamBuyCardService();
            DataTable dtCard = useCard.GetCardByCardNumber(txtCardCode.Text);
            if (Convert.ToBoolean(dtCard.Rows[0]["IsUsed"]) == false)
            {
                ltlCardNumber.Text = dtCard.Rows[0]["CardNumber"].ToString();
                ltlCardPrice.Text = dtCard.Rows[0]["CardPrice"].ToString();

                if (Convert.ToDouble(ltlCardPrice.Text) != 0)
                {

                    ltlTotal.Text = (Convert.ToInt32(txtQuantity.Text) * Convert.ToDouble(ltlPrice.Text)).ToString();
                    ltlTotalAll.Text = (Convert.ToInt32(txtQuantity.Text) * Convert.ToDouble(ltlPrice.Text) - Convert.ToDouble(ltlCardPrice.Text) + Convert.ToDouble(ltlTransferFee.Text)).ToString();
                }
            }
            else
            {
                Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Error;
                Main_PublicSystemMessage1.MessageVisible = true;
                Main_PublicSystemMessage1.Message = "该礼品卡已经使用过";
            }
        }

        protected void btnPay_Click(object sender, EventArgs e)
        {
            if (txtMobile.Text == "")
            {
                Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Error;
                Main_PublicSystemMessage1.MessageVisible = true;
                Main_PublicSystemMessage1.Message = "请输入正确的11位手机号码";
            }
            else if (txtQuantity.Text == "0")
            {
                Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Error;
                Main_PublicSystemMessage1.MessageVisible = true;
                Main_PublicSystemMessage1.Message = "购买数量不能为0";
            }
            else if (new TeamBuy.AccessService.TeamBuyDealService().GetLeftDealNumber(currentDealID) - Convert.ToInt32(txtQuantity.Text) < 0)
            {
                Main_PublicSystemMessage1.MessageType = Common.MessageTypes.Error;
                Main_PublicSystemMessage1.MessageVisible = true;
                Main_PublicSystemMessage1.Message = "剩余数量不足，只能购买" + new TeamBuy.AccessService.TeamBuyDealService().GetLeftDealNumber(currentDealID) + "个";
            }
            else
            {
                double allPrice = Convert.ToInt32(txtQuantity.Text) * Convert.ToDouble(ltlPrice.Text) - Convert.ToDouble(ltlCardPrice.Text) + Convert.ToDouble(ltlTransferFee.Text);
                TeamBuy.AccessService.TeamBuyOrderService createOrder = new AccessService.TeamBuyOrderService();
                int orderID = createOrder.CreateOrder(0, currentDealID, Convert.ToInt32(txtQuantity.Text), Convert.ToDouble(ltlTotalAll.Text), ltlCardPrice.Text == "0" ? false : true, ltlCardNumber.Text, txtMobile.Text);
                Response.Redirect("/Account/OrderPay.aspx?Order=" + Common.PublicFunction.DESEncrypt(orderID.ToString()));
            }
        }
    }
}